package com.hmall.gateway.filter;

import com.hmall.common.filter.ThreadLocalUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


// 顺序值
/*
@Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter {
    */
/**
     * @param exchange  封装了request  response
     * @param chain  过滤器链
     * @return
     *//*

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1 . 获取request
        ServerHttpRequest request = exchange.getRequest();
        //2. 获取 authorization参数
        String authorization = request.getQueryParams().getFirst("authorization");
        if("2".equals(authorization)){
            //4. 如果是  就放行
            ThreadLocalUtil.setUserId(Long.parseLong(authorization));
            return chain.filter(exchange);
        }
        //如果不是 就拦截终止请求
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        return response.setComplete();
    }
}
*/
